package com.xly.webclick.adapter;

import android.databinding.ObservableList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Created by amos on 2018/5/25.
 */

public class MergeList {

    public static class MergeMapItem{
        public final int layoutId;
        public final int brId;

        public MergeMapItem(int layoutId, int brId) {
            this.layoutId = layoutId;
            this.brId = brId;
        }
    }

    private final ArrayList<List<Object>> lists = new ArrayList<>(2);


    private final ArrayList<Class<Object>> classList = new ArrayList<>(2);


    private final ArrayList<MergeMapItem> itemBinds = new ArrayList<>(2);


    public void insertItem(int pos, Object item) {
        lists.add(pos, Collections.singletonList(item));
    }

    public void insertItem(Object item) {
        lists.add(Collections.singletonList(item));
    }

    public void insertList(List list) {
        lists.add(list);
    }


    public Object get(int location) {
        if (location < 0) {
            throw new IndexOutOfBoundsException();
        }

        int size = 0;

        for (int i = 0, listsSize = lists.size(); i < listsSize; i++) {
            List list = lists.get(i);
            if (location - size < list.size()) {
                return list.get(location - size);
            }
            size += list.size();
        }
        throw new IndexOutOfBoundsException();
    }


    public void removeItem(Object object) {
        for (int i = 0, listsSize = lists.size(); i < listsSize; i++) {
            List list = lists.get(i);
            if (!(list instanceof ObservableList)) {
                Object item = list.get(0);
                if ((object == null) ? (item == null) : object.equals(item)) {
                    lists.remove(i);
                    return;
                }
            }
        }
    }


    public boolean removeList(List<Object> listToRemove) {
        for (int i = 0, listsSize = lists.size(); i < listsSize; i++) {
            List list = lists.get(i);
            if (list == listToRemove) {
                lists.remove(i);
                return true;
            }
        }
        return false;
    }


    public void removeAll() {
        int size = size();
        if (size == 0) {
            return;
        }
        lists.clear();
    }


    public MergeMapItem getMergeItems(int location) {
        if (location < 0 || location > size() - 1) {
            return null;
        }

        Class claz = get(location).getClass();

        int index = classList.indexOf(claz);

        if (index >= 0) {
            return itemBinds.get(index);
        }

        return null;
    }



    public MergeList map(Class claz, MergeMapItem item) {

        int index = classList.indexOf(claz);
        if (index >= 0) {
            itemBinds.set(index, item);
        } else {
            classList.add(claz);
            itemBinds.add(item);
        }

        return this;
    }


    public int size() {
        int size = 0;
        for (int i = 0, listsSize = lists.size(); i < listsSize; i++) {
            List list = lists.get(i);
            size += list.size();
        }
        return size;
    }
}
